package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Permission;
import com.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 张波
 * @Time: 2025/8/28 14:45
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
	private final PermissionService permissionService;

	public PermissionController(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	@RequestMapping("/findAll")
	public String findAll(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "5")  int limit){
		PageHelper.startPage(page,limit);
		List<Permission> all = permissionService.findAll();
		PageInfo<Permission> pageInfo = new PageInfo<>(all);
		request.setAttribute("pageInfo",pageInfo);
		return "/permission/permission-list";
	}
	@RequestMapping("/add")
	public String add(String permissionName,String url){
		permissionService.addPermission(permissionName,url);
		return "redirect:findAll";
	}
	/// 根据id进行查询
	@RequestMapping(value = "/findById",method = RequestMethod.GET)
	public String findById(HttpServletRequest request,int id){
		Permission byId = permissionService.findById(id);
		request.setAttribute("permission",byId);
		return "/permission/permission-edit";
	}
	/// 根据id进行修改
	@RequestMapping(value = "/edit",method = RequestMethod.GET)
	public String updateById(Permission permission){
		permissionService.updatePermission(permission);
		return "redirect:findAll";
	}
	/// 根据id进行删除
	@RequestMapping("/deleteById")
	public String deleteById(int id){
		permissionService.deletePermission(id);
		return "redirect:findAll";
	}
}
